#include "button_app.h"
#include "gpio.h"
#include "tim14.h"

enum key_id {
    key1_id,
    key2_id,
};
static struct Button key1, key2;

static void key1_press_down_handle(void* btn);
static void key1_press_up_handle(void* btn);
// static void key1_press_repeat_handle(void* btn);
// static void key1_single_click_handle(void* btn);
// static void key1_double_click_handle(void* btn);
// static void key1_long_press_start_handle(void* btn);
static void key1_long_press_hold_handle(void* btn);

// static void key2_press_down_handle(void* btn);
// static void key2_press_up_handle(void* btn);
// static void key2_press_repeat_handle(void* btn);
static void key2_single_click_handle(void* btn);
static void key2_double_click_handle(void* btn);
static void key2_long_press_start_handle(void* btn);
// static void key2_long_press_hold_handle(void* btn);

static uint8_t read_button_GPIO(uint8_t button_id)
{
    // you can share the GPIO read function with multiple Buttons
    switch(button_id)
    {
        case key1_id:
            return HAL_GPIO_ReadPin(Key1_GPIO_PORT, Key1_GPIO_PIN);
        case key2_id:
            return HAL_GPIO_ReadPin(Key2_GPIO_PORT, Key2_GPIO_PIN);
        default:
            return 0;
    }
}

void button_app_init(void)
{
    PY_GPIO_Init();
    button_init(&key1, read_button_GPIO, 0, key1_id);
    button_init(&key2, read_button_GPIO, 0, key2_id);

    button_attach(&key1, PRESS_DOWN, key1_press_down_handle);
    button_attach(&key1, PRESS_UP, key1_press_up_handle);
    // button_attach(&key1, PRESS_REPEAT, key1_press_repeat_handle);
    // button_attach(&key1, SINGLE_CLICK, key1_single_click_handle);
    // button_attach(&key1, DOUBLE_CLICK, key1_double_click_handle);
    // button_attach(&key1, LONG_PRESS_START, key1_long_press_start_handle);
    button_attach(&key1, LONG_PRESS_HOLD, key1_long_press_hold_handle);

    // button_attach(&key2, PRESS_DOWN, key2_press_down_handle);
    // button_attach(&key2, PRESS_UP, key2_press_up_handle);
    // button_attach(&key2, PRESS_REPEAT, key2_press_repeat_handle);
    button_attach(&key2, SINGLE_CLICK, key2_single_click_handle);
    button_attach(&key2, DOUBLE_CLICK, key2_double_click_handle);
    button_attach(&key2, LONG_PRESS_START, key2_long_press_start_handle);
    // button_attach(&key2, LONG_PRESS_HOLD, key2_long_press_hold_handle);

    button_start(&key1);
    button_start(&key2);

    PY_TIM14_Init();
}

static void key1_press_down_handle(void* btn)
{
    LED2(LED_ON);
}

static void key1_press_up_handle(void* btn)
{
    LED2(LED_OFF);
    LED3(LED_OFF);
}

static void key1_long_press_hold_handle(void* btn)
{
    static uint32_t cnt = 0;
    if(cnt++ >= 100) {      // 100 * 5ms = 0.5s
        cnt = 0;
        LED3_TOGGLE();
    }
}


static void key2_single_click_handle(void* btn)
{
    LED3(LED_ON);
}

static void key2_double_click_handle(void* btn)
{
    LED3(LED_OFF);
}

static void key2_long_press_start_handle(void* btn)
{
    LED4_TOGGLE();
}